package com.java.code.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class JsonUtils {
    public static Map<String, Object> map = new HashMap() {
        {
            put("name", "张三");
            put("age", 1);
            put("sex", "F");
            put("hobby", new ArrayList<String>() {
                {
                    add("打篮球");
                    add("排球");
                    add("羽毛球");
                    add("下棋");
                }
            });
            put("father", new HashMap<String, Object>() {{
                put("name", "张三father");
                put("age", "30");
            }});
        }
    };

    public static String json = "{\"sex\":\"F\",\"father\":{\"name\":\"张三father\",\"age\":\"30\"},\"name\":\"张三\",\"age\":1,\"hobby\":[\"打篮球\",\"排球\",\"羽毛球\",\"下棋\"]}";


    /**
     * 将对象转成Json字符串
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public static String toJsonString(Object obj) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(obj);
        return s;
    }

    /**
     * 将Json转化成对象
     *
     * @throws Exception
     */
    @Test
    public void parseJson() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(map);
        System.out.println(s);

        Map result = mapper.readValue(json, Map.class);
        System.out.println(result);
    }

    /**
     * 获取单个节点
     *
     * @throws Exception
     */
    @Test
    public void readNode() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        //转化成JsonNode树结构
        JsonNode jsonNode = mapper.readTree(json);
        //获取sex节点得
        System.out.println(jsonNode.path("sex").asText());
        //获取father.name节点
        System.out.println(jsonNode.path("father").path("name").asText());

        JsonParser traverse = jsonNode.traverse();


        if (traverse.nextToken() != JsonToken.START_OBJECT) {
            throw new Exception("");
        }
        while (traverse.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = traverse.getCurrentName();
            System.out.println(fieldName);
            //if (...){...}
            traverse.nextToken();
        }
        traverse.close();
    }

    /**
     * 获取所有的节点
     */
    @Test
    public void readAllNode() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        //转化成JsonNode树结构
        JsonNode jsonNode = mapper.readTree(json);
        JsonParser traverse = jsonNode.traverse();
        if (traverse.nextToken() != JsonToken.START_OBJECT) {
            throw new Exception("");
        }
        while (traverse.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = traverse.getCurrentName();
            System.out.println(fieldName + ":" + jsonNode.get(fieldName).asText());
            traverse.nextToken();
        }
        traverse.close();
    }

}
